package com.tlr.device.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tfb.dao.AssessmentMapper;
import com.tfb.dao.DeviceDataMapper;
import com.tfb.dao.DeviceMapper;
import com.tfb.dao.DeviceUserBindMapper;
import com.tfb.dao.HealthAlertMapper;
import com.tfb.dao.HealthUserMapper;
import com.tfb.dao.LocationDataMapper;
import com.tfb.dao.RelationUserMapper;
import com.tfb.dao.UsersMapper;
import com.tfb.rong.Device;
import com.tfb.rong.DeviceDataWithBLOBs;
import com.tfb.rong.HealthAlert;
import com.tfb.rong.LocationData;
import com.tlr.device.model.LocationCard;

@Service("deviceDatabaseService")
public class DeviceDatabaseService {

    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    @Autowired
    private LocationDataMapper locationDataMapper;
    @Autowired
    private AssessmentMapper assessMapper;
    @Autowired
    private HealthAlertMapper alertMapper;
    @Autowired
    private RelationUserMapper relationUserMapper;
    @Autowired
    private DeviceUserBindMapper deviceUserBindMapper;
    @Autowired
    private HealthUserMapper healthUserMapper;
    @Autowired
    private UsersMapper userMapper;

    public HealthAlertMapper getAlertMapper() {
        return alertMapper;
    }

    public DeviceDataMapper getDeviceDataMapper() {
        return deviceDataMapper;
    }

    public UsersMapper getUserMapper() {
        return userMapper;
    }

    public HealthUserMapper getHealthUserMapper() {
        return healthUserMapper;
    }

    public DeviceMapper getDeviceMapper() {
        return deviceMapper;
    }

    public DeviceUserBindMapper getDeviceUserBindMapper() {
        return deviceUserBindMapper;
    }

    public RelationUserMapper getRelationUserMapper() {
        return relationUserMapper;
    }

    public List<HealthAlert> selectAllAlertByStatus(Integer status) {
        return alertMapper.selectAllAlertByStatus(status);
    }

    public int updateByPrimaryKeySelective(HealthAlert record) {
        return alertMapper.updateByPrimaryKeySelective(record);
    }

    public int insertHealthDeviceAlert(HealthAlert dd) {
        return alertMapper.insertSelective(dd);
    }

    public int insertDeviceDataSelective(DeviceDataWithBLOBs dd) {
        return deviceDataMapper.insertSelective(dd);
    }

    public int deleteDataBefore6Hours(DeviceDataWithBLOBs dd) {
        return deviceDataMapper.deleteDataBefore6Hours(dd);
    }

    public List<DeviceDataWithBLOBs> getDeviceData(DeviceDataWithBLOBs dd) {
        return deviceDataMapper.selectLastDeviceData(dd);
    }

    //get a user binded special device
    public List<Device> getAllBindDevice(String strUser, int type) {
        Device device = new Device();
        device.setBinduser(strUser);
        device.setDevicetype(type);
        return deviceMapper.selectAllActiveDevice(device);
    }

    public List<Device> getAllBindDevice(Device device) {
        return deviceMapper.selectAllActiveDevice(device);
    }

    public List<Device> getDevices(Device device) {
        return deviceMapper.selectDevices(device);
    }

    public List<Device> getDevices(int type) {
        Device device = new Device();
        device.setDevicetype(type);
        return deviceMapper.selectDevices(device);
    }

    public List<Device> getAllDevice(int type, int status) {
        Device device = new Device();
        device.setDevicetype(type);
        device.setStatus(status);
        return deviceMapper.selectDevices(device);
    }

    public Device getDeviceInfo(String deviceID) {
        Device device = new Device();
        device.setDeviceid(deviceID);
        return deviceMapper.selectDeviceInfo(device);
    }

    public List<Device> getAllDeviceBasicInfo(String strUser, int type) {
        Device device = new Device();
        device.setBinduser(strUser);
        device.setDevicetype(type);
        return deviceMapper.selectBasicActiveDevice(device);
    }

    public List<Device> getAllDevice() {
        return deviceMapper.selectAllDevice();
    }

    public Device getDeviceByMac(String mac) {
        Device device = new Device();
        device.setDevicemac(mac);
        List<Device> devices = deviceMapper.selectDevicesByMac(device);
        return devices.isEmpty() ? null : devices.get(0);
    }

    //get one device current location data(last one hour)
    public List<LocationCard> getCurrentLocationData(String strid) {
        List<LocationData> ls = locationDataMapper.selectAllData(strid);
        List<LocationCard> dataList = new ArrayList<>();
        for (LocationData l : ls) {
            LocationCard lcard = new LocationCard();
            lcard.setLat(l.getLat());
            lcard.setLng(l.getLng());
            dataList.add(lcard);
        }
        return dataList;
    }

    public int addDevice(Device device) {
        device.setDeviceid(String.format("D%06d", assessMapper.getNextID("device_id")));
        return deviceMapper.insert(device);
    }

    public int modifyDevice(Device device) {
        return deviceMapper.updateByPrimaryKey(device);
    }

    public int delDevice(String deviceid) {
        return deviceMapper.deleteByPrimaryKey(deviceid);
    }

    public int updateDevice(Device device) {
        return deviceMapper.updateByPrimaryKeySelective(device);
    }

}
